<html>
<div id="myheader">
<?php
session_start();

error_reporting(E_ALL);
?>
</div>


<title>OSPMS</title>
<head>



</head>


<div id="mybody">



<!-- <form method="post" enctype="multipart/form-data" action="<?php $_PHP_SELF ?>"> -->
<form method="post" enctype="multipart/form-data" action="Upload_Project.php">
<table class="mytable regtable">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
    <tr>
      <th></th>
      <th class="righttd">Upload File</th>

    </tr>
<!--
    <tr>
      <td class="lefttd">cust_id:</td>
      <td class="righttd"><input type="text" name="cust_id" class="text"/></td>
    </tr>
    <tr>
      <td class="lefttd">Input1:</td>
      <td class="righttd"><input type="text" name="Input1" class="text"/></td>
    </tr>
-->
    <tr>
      <td class="lefttd">File:</td>
      <td class="righttd"><input type="file" name="Input_file" id="filename" style="display:button"/></td>
<!--	<button  class="righttd" type="button" onclick="javascript:document.getElementById('myUpload').click();">Upload File</button>  -->
    </tr>
    <tr>
      <td class="lefttd">Notes:</td>
      <td class="righttd"><input type="text" name="Input3" class="text"/></td>
    </tr>
    <tr>
      <td></td>
      <td class="righttd">
        <input type="submit" value="Upload" name="add" id="cmdLogin"/>
      </td>
    </tr>
</table>
</form>



<?php

// Insert the path where you unpacked log4php
include('log4php/Logger.php');

    // Tell log4php to use our configuration file.
    Logger::configure('config.xml');
     
    // Fetch a logger, it will inherit settings from the root logger
    $log = Logger::getLogger('/var/log/OSPMS/portal/myLogger');
#    $log = Logger::getLogger('/var/log/OSPMS/portal/myLog.log');
     
    $log->warn("My Warn message."); 

define("UPLOAD_DIR", "/var/www/uploads/SAVED/");
 
    $input_file = $_FILES["Input_file"]; 
    $log->warn("input filename: $input_file"); 
    $input_file2 = $_FILES["Input_file"]; 
    $input_file2_name = $input_file2['name'];
    $log->warn("input filename: ".$input_file2_name); 
    $log->warn("This id : ".$_POST['cust_id']);
#    $log->warn('_FILE array'); 
#    $log->warn($_FILES); 
    $log->warn('UPLOADED _FILE array'); 
    $log->warn($_FILES['Input_file']); 
    $log->warn("This file with tempname ".$_FILES['Input_file']['tmp_name']);
#    $log->warn('POST array'); 
#    $log->warn($_POST); 

if (!empty($_FILES['Input_file'])) {

    $myFile = $_FILES["Input_file"];

    $input_file2 = $_FILES["Input_file"]; 
    $input_file2_name = $input_file2['name'];
    $log->warn("input filename:: ".$input_file2_name); 
 
    if ($myFile["error"] !== UPLOAD_ERR_OK) {
#        echo "<p>An error occurred.</p>";
        $log->warn("An error occurred"); 
        exit;
    }
 
    // ensure a safe filename
    $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
    $input_file2_name3 = $input_file2['name'];
    $log->warn("input filename::: ".$name); 
    $log->warn("input filename::: ".$input_file2_name3); 
 
    // don't overwrite an existing file
    $i = 0;
    $parts = pathinfo($name);
    while (file_exists(UPLOAD_DIR . $name)) {
        $i++;
        $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
    }
 
    // preserve file from temporary directory
    $success = move_uploaded_file($myFile["tmp_name"],
        UPLOAD_DIR . $name);
    if (!$success) {
#        echo "<p>Unable to save file.</p>";
        $log->warn("An error occurred"); 
        exit;
    }
 
// configuration
$dbtype     = "mysql";
$dbhost     = "localhost";
$dbname     = "OSPMS";
$dbuser     = "ospms";
$dbpass     = "ospms";

#var_dump($_SESSION);
$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'];
 
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
 
// new data
$title = $name;
$author = "/var/www/uploads/SAVED/";
$id = 1;
$fo_id = 1;
$s_id = 1;
 
// query
$sql = "INSERT INTO user_uploaded_files (user_id, folder_id, study_id, file_name, file_location) VALUES (:id, :fo_id, :s_id, :title, :auth)";
$q = $conn->prepare($sql);
$q->execute(array(':id'=>$id,
		  ':fo_id'=>$fo_id,
		  ':s_id'=>$s_id,
		  ':auth'=>$author,
                  ':title'=>$title));
 


    // set proper permissions on the new file
    chmod(UPLOAD_DIR . $name, 0644);
    $log->warn("input filename::: ".$name); 
    $log->warn("input filename::: ".$input_file2_name3); 


    echo " <script> window.location.replace('UserFiles.php') </script> ";

}  # Big IF

#    asof 20141109 header() function must be called before any screen output
#    apperantly the log facility breaks that
#    header("Location: UserFiles.php");
#    header("Location: index.php");
#    header("Location: /OSPMS/ShowUser.php");


    $log->warn(":::logentry:::"); 
?>

<?php
#if(isset($_POST['add']))
#{
#
#$dbhost = 'localhost';
#$dbuser = 'ospms';
#$dbpass = 'ospms';
#$conn = mysql_connect($dbhost, $dbuser, $dbpass);
#if(! $conn )
#{
#  die('Could not connect: ' . mysql_error());
#}
#
#// if(! get_magic_quotes_gpc() )
#// {
#//    $emp_name = addslashes ($_POST['emp_name']);
#//    $emp_address = addslashes ($_POST['emp_address']);
#// }
#// else
#// {
#//    $emp_name = $_POST['emp_name'];
#//    $emp_address = $_POST['emp_address'];
#// }
#// $emp_salary = $_POST['emp_salary'];
#
#$cust_id = $_FILES["Input_file"]["name"];
#$Input1 = $_FILES["Input_file"]["type"];
#$Input3 = $_FILES["Input_file"]["size"];
#$filename = $_FILES["Input_file"]["tmp_name"];
#
#$sql = "INSERT INTO customer_file_info".
#       "(cust_id, file_name, file_location, revision ) ".
#       "VALUES('$cust_id', '$Input1', '$filename', '$Input3'";
#echo "$sql";
#echo "<br> </br>";
#mysql_select_db('OSPMS');
#$retval = mysql_query( $sql, $conn );
#
#if(! $retval )
#{
#  mysql_close($conn);
#  die('Could not enter data: ' . mysql_error());
#}
#else
#{
#echo "Entered data successfully\n";
#}
#
#}
#?>






</div>
</div>

